﻿using System.Diagnostics.Metrics;
using System.Net.Sockets;
using ModuleBase;
using Newtonsoft.Json.Linq;
using NModbus;

namespace PLCCon
{
    public class PLCCon : ModuleBase.ModuleBase
    {
        IModbusMaster master;
        TcpClient client;
        public bool Connected { get; set; }
        bool outputs = false;
        bool isExecuted = false;
        private string _parameter1;
        public string Parameter1
        {
            get => _parameter1;
            set
            {
                if (_parameter1 != value)
                {
                    _parameter1 = value;
                    OnPropertyChanged(nameof(Parameter1));
                }
            }
        }
        private int _parameter2;
        public int Parameter2
        {
            get => _parameter2;
            set
            {
                if (_parameter2 != value)
                {
                    _parameter2 = value;
                    OnPropertyChanged(nameof(Parameter2));
                }
            }
        }
        private object _parameter3;
        public object Parameter3
        {
            get => _parameter3;
            set
            {
                if (_parameter3 != value)
                {
                    _parameter3 = value;
                    OnPropertyChanged(nameof(Parameter3));
                }
            }
        }
        public override Dictionary<string, object> GetProperties()
        {
            return new Dictionary<string, object>
            {
                { "IP", Parameter1 },
                { "端口", Parameter2 },
                { "操作", Parameter3 },
            };
        }
        public override Dictionary<string, List<object>> PropertyOptions { get; } = new Dictionary<string, List<object>>
        {
            { "操作" ,new List<object>{ "连接","关闭" } },
        };
        public PLCCon()
        {
            Name = "PLCCon";

            DisplayNameToPropertyNameMap = new Dictionary<string, string>
            {
                { "IP", nameof(Parameter1) },
                { "端口", nameof(Parameter2) },
                { "操作", nameof(Parameter3) },
            };
        }
        public override bool Execute()
        {
            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
            Status = ModuleStatus.Executing; // 设置状态为执行中

            if (isExecuted)
            {
                keyValuePairs.Add("Output", true);
                return outputs;
            }

            if (Parameter3.ToString() == "连接")
            {
                bool result = Connect();
                keyValuePairs.Add("Output", result);
                isExecuted = result;
            }
            else
            {
                Close();
                keyValuePairs.Add("Output", true);
            }

            return outputs;
        }

        public override async Task<bool> ExecuteAsync()
        {
            
            var result = await Task.Run(() => Execute());
            return result;
        }

        public override void Initialize()
        {
            isExecuted = false;
        }

        public void Close()
        {
            try
            {
                Connected = false;
                master?.Dispose();
                client?.Close();
                client?.Dispose();
            }
            catch { }
        }

        public bool Connect()
        {
            try
            {
                client = new TcpClient(Parameter1, Parameter2);
                var factory = new ModbusFactory();
                master = factory.CreateMaster(client);
                Connected = true;
                return true;
            }
            catch (Exception ex)
            {
                Connected = false;
                return false;
            }
        }
    }
}
